package com.jader.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import com.jader.util.CommonUtils;
import com.jader.util.JaderApplication;
import com.zlp.jader.R;

public class ForgetActivity extends Activity implements OnClickListener {
	Button btn_forget_sendmsg, btn_forget_next;
	EditText edit_phone, edit_code;
	String phoneNum = "";
	int i = 30;
	ImageView forget_back;
	TextView forget_title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget);
		initSDK();
		initView();
	}

	/** 初始化短信APISDK */
	private void initSDK() {
		// 启动短信验证sdk
		SMSSDK.initSDK(this, JaderApplication.getAPPKEY(),
				JaderApplication.getAPPSECRRET());
		EventHandler eventHandler = new EventHandler() {
			@Override
			public void afterEvent(int event, int result, Object data) {
				Message msg = new Message();
				msg.arg1 = event;
				msg.arg2 = result;
				msg.obj = data;
				handler.sendMessage(msg);
			}
		};
		// 注册回调监听接口
		SMSSDK.registerEventHandler(eventHandler);
	}

	/**
	 * 初始化视图
	 */
	private void initView() {
		btn_forget_sendmsg = (Button) findViewById(R.id.btn_forget_send_msg);
		btn_forget_next = (Button) findViewById(R.id.btn_forget_next);
		edit_phone = (EditText) findViewById(R.id.edit_forget_phone);
		edit_code = (EditText) findViewById(R.id.edit_forget_code);
		forget_back = (ImageView) findViewById(R.id.top_actionbar_back);
		forget_title = (TextView) findViewById(R.id.top_actiobar_text);
		forget_title.setText("获取验证码");
		forget_back.setOnClickListener(this);
		btn_forget_next.setOnClickListener(this);
		btn_forget_sendmsg.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		phoneNum = edit_phone.getText().toString().trim();
		switch (v.getId()) {
		case R.id.btn_forget_send_msg:
			// 1. 通过规则判断手机号
			if (!CommonUtils.judgePhoneNums(phoneNum)) {
				return;
			}
			// 2. 通过sdk发送短信验证
			SMSSDK.getVerificationCode("86", phoneNum);
			// 3. 把按钮变成不可点击，并且显示倒计时（正在获取）
			btn_forget_sendmsg.setClickable(true);
			btn_forget_sendmsg.setText("重新发送(" + i + ")");
			new Thread(new Runnable() {
				@Override
				public void run() {
					for (; i > 0; i--) {
						handler.sendEmptyMessage(-9);
						if (i <= 0) {
							break;
						}
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					handler.sendEmptyMessage(-8);
				}
			}).start();
			break;
		case R.id.btn_forget_next:
			SMSSDK.submitVerificationCode("86", phoneNum, edit_code.getText()
					.toString());
			break;
		case R.id.top_actionbar_back:
			startActivity(new Intent(this, LoginActivity.class));
			break;
		default:
			break;
		}
	}

	/** 处理短信注册的handle */
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == -9) {
				btn_forget_sendmsg.setText("重新发送(" + i + ")");
			} else if (msg.what == -8) {
				btn_forget_sendmsg.setText("获取验证码");
				btn_forget_sendmsg.setClickable(true);
			} else {
				int event = msg.arg1;
				int result = msg.arg2;
				Object data = msg.obj;
				Log.e("event", "event=" + event);
				if (result == SMSSDK.RESULT_COMPLETE) {
					// 短信注册成功后，返回MainActivity,然后提示
					if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
						// 提交验证码成功
						JaderApplication.show(getBaseContext(), "提交验证码成功");
						Intent intent = new Intent(ForgetActivity.this,
								SetPassWordActivity.class);
						intent.putExtra("phoneNum", phoneNum);
						startActivity(intent);
					} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
						JaderApplication.show(getBaseContext(), "验证码在路上了,不要着急");
					} else {
						((Throwable) data).printStackTrace();
					}
				}
			}
		}
	};

	@Override
	protected void onDestroy() {
		SMSSDK.unregisterAllEventHandler();
		super.onDestroy();
	}
}
